//
//  XJRequestFailView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/8.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJRequestFailView.h"

@interface XJRequestFailView()
@property (nonatomic, strong) UIView *bgView; //背景view
@property (nonatomic, strong) UIImageView *errorIv; //网络连接失败
@property (nonatomic, strong) UIButton *reTryButton; //重试button

@end

@implementation XJRequestFailView

#pragma mark - 一 lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}

- (UIImageView *)errorIv {
    if (!_errorIv) {
        _errorIv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"networkError"]];
    }
    return _errorIv;
}

- (UIButton *)reTryButton {
    if (!_reTryButton) {
        _reTryButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _reTryButton.backgroundColor = XJCOLOR_RGB(255, 218, 67);
        [_reTryButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_reTryButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];

        [_reTryButton setTitle:@"重试" forState:(UIControlStateNormal)];
        [_reTryButton addTarget:self action:@selector(reTryButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        _reTryButton.layer.masksToBounds = YES;
        _reTryButton.layer.cornerRadius = 10;
    }
    return _reTryButton;
}

#pragma mark - 二 init

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.errorIv];
    [self.bgView addSubview:self.reTryButton];
    
    self.backgroundColor = [UIColor whiteColor];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    
    [self.errorIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(100);
        
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(200, 200));
    }];
    
    [self.reTryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.top.equalTo(self.errorIv.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(60, 40));
    }];
    
}
#pragma mark - 三 点击事件

- (void)reTryButtonAction:(UIButton *)button {
    
    if (self.retryBtnBlock) {
        self.retryBtnBlock();
    }
}
@end
